package active;

import com.itany.jdbc.factory.ObjectFactory;
import pojo.User;
import service.UserService;
import service.proxy.ProxyTransaction;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 陈华旺
 * @version 1.0
 * @date 2023年11月22日 11:55
 * @Description TODO
 */
// * 表示任意路径
//@WebServlet("*.user")
@WebServlet("/user/*")
public class Test13User extends HttpServlet {
    private UserService us = ObjectFactory.getObj("userServic");
    private UserService usProxy = ObjectFactory.getProxy("userServic",new ProxyTransaction(us));

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String method = req.getMethod();
        switch (method){
            case "GET" : doGet(req,resp); break;
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println( req.getServletPath() );
////        getPathInfo() 获取 在路径中通过 * 方式构建的任意路径值 - 路径描述值
//        System.out.println( req.getPathInfo() );
        String info = req.getPathInfo();
        if("/all".equals(info)){
//            查询所有的用户
            List<User> userList = usProxy.findUserList();
            req.setAttribute("userList",userList);
            req.setAttribute("path","users.jsp");
    //        req.getRequestDispatcher("/WEB-INF/users.jsp").forward(req,resp);
    //        转发到由include组合的home.jsp
            req.getRequestDispatcher("/WEB-INF/home.jsp").forward(req,resp);
        }else if("/add".equals(info)){
            req.setAttribute("path","addUser.jsp");
//           添加新的用户
            req.getRequestDispatcher("/WEB-INF/home.jsp").forward(req,resp);
        }else{
            req.getRequestDispatcher("/WEB-INF/404.jsp").forward(req,resp);
        }
    }
}








